I was once Vice President of a Bank and was responsible for managing our liquidity. Banks can’t just make money out of thin air. There is a balance between how much of your deposits on hand you can loan out, how much are kept in overnight funds (invested overnight), how much is in short term investments. Also you have to manage the loan terms and your deposit terms. If a bank makes too many long term fixed rate loans but doesn’t match that up with long term fixed rate deposits it can be disastrous. In this scenario your deposits would reprice faster than your loans resulting in greater costs(interest paid to customers on deposits) than income ( interest paid to the bank on loans.)
The interest charged on loans vs the interest paid out on deposits is the spread which a bank pays its operating costs from. If the bank doesn’t have the loan volume needed then they invest the extra deposits to bring in more income. Obviously this is the simplified version, but at no point does the bank make money out of thin air.
There are regulations that limit what types of investments the bank can use in order to protect deposits. Banks are always trying to push the envelope on what investments qualify to increase their profits.